/******************************************************************************
 *  本代码实现,摘录自<javascript数据结构与算法>第3章列表
 *  个人认为其实现并不好:
 *      * api设计跟jdk中list存在差距
 *      * 书上的勘误也有
 *
 */

/**
 * 列表实现
 * @constructor
 */
function List() {
    //---------------属性------------------------------------------------
    this.listSize = 0;
    this.pos = 0;
    this.dataStore = []; // 初始化一个空数组来保存列表元素

    //---------------方法------------------------------------------------
    this.clear = clear;
    this.find = find;
    this.toString = toString;
    this.insert = insert;
    this.append = append;
    this.remove = remove;
    this.front = front;
    this.end = end;
    this.prev = prev;
    this.next = next;
    this.length = length;
    this.currPos = currPos;
    this.moveTo = moveTo;
    this.getElement = getElement;
    this.contains = contains;

    /**
     * 给列表添加元素
     * @param element
     */
    function append(element) {
        this.dataStore[this.listSize++] = element;
    }

    /**
     * 在列表中查找某一元素
     * @param element
     * @returns {number}
     */
    function find(element) {
        for (var i = 0; i < this.dataStore.length; ++i) {
            if (this.dataStore[i] == element) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 从列表中删除元素
     * @param element
     * @returns {boolean}
     */
    function remove(element) {
        var foundAt = this.find(element);
        if (foundAt > -1) {
            this.dataStore.splice(foundAt, 1);
            --this.listSize;
            return true;
        }
        return false;
    }

    /**
     * 列表中有多少个元素
     * @returns {number}
     */
    function length() {
        return this.listSize;
    }

    /**
     * 显示列表中的元素
     * @returns {Array}
     */
    function toString() {
        return this.dataStore;
    }

    /**
     * 向列表中插入一个元素
     * @param element
     * @param after
     * @returns {boolean}
     */
    function insert(element, after) {
        var insertPos = this.find(after);
        if (insertPos > -1) {
            this.dataStore.splice(insertPos + 1, 0, element);
            ++this.listSize;
            return true;
        }
        return false;
    }

    /**
     * 清空列表中所有的元素
     */
    function clear() {
        delete this.dataStore;
        this.dataStore = [];
        this.listSize = this.pos = 0;
    }

    /**
     * 判断给定值是否在列表中
     * @param element
     * @returns {boolean}
     */
    function contains(element) {
        for (var i = 0; i < this.dataStore.length; ++i) {
            if (this.dataStore[i] == element) {
                return true;
            }
        }
        return false;
    }

    function front() {
        this.pos = 0;
    }

    function end() {
        this.pos = this.listSize - 1;
    }

    function prev() {
        if (this.pos > 0) {
            --this.pos;
        }
    }

    function next() {
        if (this.pos < this.listSize - 1) {
            ++this.pos;
        }
    }

    function currPos() {
        return this.pos;
    }

    function moveTo(position) {
        this.pos = position;
    }

    function getElement() {
        return this.dataStore[this.pos];
    }
}

function print(x) {
    console.log(x);
}
var names = new List();
names.append("Cynthia");
names.append("Raymond");
names.append("Barbara");
print(names.toString());
names.remove("Raymond");
print(names.toString());